package org.example.book.service.impl;

import org.example.book.entity.Book;
import org.example.book.mapper.BookMapper;
import org.example.book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    public List<Book> getBooks(){
        return bookMapper.getBooks();
    }

    public Book getBookById(Long id){
        return bookMapper.getBookById(id);
    }

    @Override
    public List<Book> getBooksByConditions(String type, String name, String information) {
        return bookMapper.getBooksByConditions(type, name, information);
    }

    @Override
    public Book updateBook(Book book) {
        int rows = bookMapper.updateBook(book);
        if (rows > 0) {
            return bookMapper.getBookById(book.getId());
        } else {
            throw new RuntimeException("更新失败，书籍ID不存在");
        }
    }

    @Override
    public int deleteBook(Long id) {
        System.out.println("要删除的书籍ID: " + id); // 打印要删除的书籍ID，确认是否正确
        int rows = bookMapper.deleteBook(id);
        System.out.println("删除操作影响的行数: " + rows); // 打印删除操作影响的行数，确认是否删除成功
        return rows;
    }

    @Override
    public Book addBook(Book book) {
        bookMapper.addBook(book);
        return book;
    }
}
